<%@ page language="java" pageEncoding="UTF-8" %>
<html>
<head>
	<jsp:include page="/WEB-INF/jsp/ApnsHeader.jsp" />
	<script type="text/javascript">
	function doLoadAppList()
	{
		APNS.util.doAjax({
			url: APNS.context.getContextUrl( "/ajaxList.sd"),
			data: {
				funcId:"appList"
			},
			complete:function(req, status) {
				if(status == "success")
				{
					var result = JSON.parse(req.responseText);
					var appList = result.appList.list;
					var $appListBody = $("#appListTable tbody");
					$appListBody.empty();
					if(appList.length > 0)
					{
						for(var i = 0; i < appList.length; i++)
						{
							var appId = appList[ i].appId;
							var appName = appList[ i].appName;
							var appDesc = appList[ i].appDesc;
							$appListBody.append("<tr>");
							$appListBody.append("<td><input type=\"checkbox\" class=\"checkBoxRow\" value=\"" + appId + "\" /></td>");
							$appListBody.append("<td>"+ appId +"</td>");
							$appListBody.append("<td>"+ appName +"</td>");
							$appListBody.append("<td>"+ appDesc +"</td>");
							$appListBody.append("</tr>");
						}
					}
					else
					{
						$appListBody.html("<tr><td colspan=\"3\">등록된 앱이 없음</td></tr>");
					}
				}
			}
		});
	}
	
	function doSaveApp()
	{
		APNS.util.doAjaxForm( "appSaveForm", {
			url: APNS.context.getContextUrl("/propertySet/appSave.sd"),
			complete: function(req, status) {
				if(status == "success")
				{
					alert(req.responseText);
					doLoadAppList();
				}
				$("#appSaveForm").clearForm();
			}
		});
	}
	
	function doDeleteApp()
	{
		var appIdArray = $("#appListTable tbody td .checkBoxRow:checked").toArray();
		if(appIdArray == null || appIdArray.length == 0)
		{
			alert("선택된 값이 없습니다.");
			return;
		}
		
		if(!confirm("정말 삭제하시겠습니까?"))
		{
			return;
		}
		APNS.util.doAjax({
			url: APNS.context.getContextUrl( "/deleteApp.sd"),
			data: {
				appIdCommaString: APNS.form.toStringArray( appIdArray, ",")
			},
			complete: function(req, status) {
				alert(req.responseText);
				doLoadAppList();
			}
		});
	}
	
	$( document).ready( function(){
		doLoadAppList();
		
		$("#checkAll").click(function(){
			if( $( this).is( ":checked")) 
			{
				$( "#appListTable tbody td .checkBoxRow").each( function(index){
					this.checked = true;
				});
			}
			else
			{
				$( "#appListTable tbody td .checkBoxRow").each( function(index){
					this.checked = false;
				});
			}
		});
		
	});
	</script>
</head>
<body>
<jsp:include page="/WEB-INF/jsp/ApnsTopInfo.jsp" />

<h2>1.App List</h2>

<div id="appListPannel">
	<div id="appListTableButton">
		<input type="button" value="삭제" onclick="doDeleteApp()" />
	</div>
	<table id="appListTable">
	<thead>
	<tr>
		<th><input type="checkbox" name="checkAll" id="checkAll" /></th>
		<th>Id</th>
		<th>Name</th>
		<th>Description</th>
	</tr>
	</thead>
	<tbody></tbody>
	</table>
</div>

<h2>2.App Registration</h2>
<form id="appSaveForm" method="post">
<ul>
	<li>App Id : <input type="text" name="appId"/></li>
	<li>App Name : <input type="text" name="appName"/></li>
	<li>App Description : <input type="text" name="appDesc"/></li>
</ul>
</form>
<div>
	<input type="button" name="buttonAppSave" value="Save App" onclick="doSaveApp()" />
</div>
</body>
</html>